<?php

namespace Home\Controller;

use Think\Controller;

class CommonController extends Controller
{
	//功能类似构造方法 率先执行的方法
	public function _initialize()
	{
		//用户的登录检测
		$id = $_SESSION['home']['uid'];
		//判断
		if (empty($id)) {
			$this->error('您还有没有登录呢!', U('Login/login'));
		}

		$this->check();
		$this->sendNews();
		$this->weather();
	}

	/**
	 * [check 查询当前用户的审查信息, 并分配信息显示]
	 */
	public function check(){
		$check = M('check');
		$user_id = $_SESSION['home']['uid'];
		$where['user_id'] = $user_id;
		$where['if_read'] = 1;
		$res = $check->field('c_id, user_id, cont_type, UNIX_TIMESTAMP(now())-del_time diff, public_time, if_read')->order('del_time desc')->where($where)->limit(8)->select();
		// 重新定义时间
		foreach ($res as $key => $value) {
			if ($value['diff']<60) {
				$res[$key]['diff'] = $value['diff'].'秒之前...';
			}else if($value['diff']<3600){
				$minu = floor($value['diff']/60);
				$res[$key]['diff'] = $minu.'分钟之前...';
			}else if($value['diff']<86400){
				$hour = floor($value['diff']/3600);
				$res[$key]['diff'] = $hour.'小时之前...';
			}else if($value['diff']<2592000){
				$day = floor($value['diff']/86400);
				$res[$key]['diff'] = $day.'天之前...';
			}else if($value['diff']<31104000){
				$mon = floor($value['diff']/2592000);
				$res[$key]['diff'] = $mon.'月之前...';
			}else{
				$year = floor($value['diff']/31104000);
				$res[$key]['diff'] = $year.'月之前...';
			}
			$res[$key]['public_time'] = date('Y-m-d H:i:s', $value['public_time']);
		}

		// 查询当前用户所有为读审查信息总数
		$resn = $check->field('count(*) num')->where("if_read='1' and user_id='$user_id'")->select();
		$resnum = $resn[0]['num'];

		if ($resnum != 0) {
			$this->assign('check_num', $resnum);
		}

		// 查询当前用户的所有审查信息的页数, 每10条为一页
		$schecknum = $check->field('count(*) schecknum')->where("user_id='$user_id'")->select();
		$spageall = ceil($schecknum[0]['schecknum']/10);
		$this->assign('spageall', $spageall);

		$this->assign('check', $res);

	}

	/**
	 * [checkAjax 页面点击查看所有审查信息, ajax,返回查询结果]
	 * @return   [有信息, ajax返回该信息内容 | 没有信息, ajax返回false]
	 */
	public function checkAjax(){
		$spage = I('post.spage');
		$limit = (($spage-1)*10).','.'10';
		$check = M('check');
		$user_id = $_SESSION['home']['uid'];
		$where['user_id'] = $user_id;
		$res = $check->field('c_id, user_id, cont_type,del_time, UNIX_TIMESTAMP(now())-del_time diff, public_time, if_read')->order('if_read desc, del_time desc')->where($where)->limit($limit)->select();

		if ($res) {
			$html = '';
			foreach ($res as $key => $value) {

				$cy = $value['cont_type']==1?'信息警告':'图片警告';
				$cc = $value['cont_type']==1?'信息':'图片';
				$cs = $value['if_read']==1?'style="color:#f78a09"':'style="color:#d5d5d5"';
				$html .= '<tr class="checktr" sid="'.$value['c_id'].'">
				    <td class="inbox-small-cells"><i class="icon-location-arrow" '.$cs.'></i></td>
				    <td class="view-message dont-show">'.$cy.'</td>
				    <td class="view-message">尊敬的'.$_SESSION['home']['uname'].': 你在'.date('Y-m-d H:i:s', $value['public_time']).'发布的'.$cc.'不合法, 已被系统删除..</td>
				    <td class="view-message text-right">'.date('Y-m-d H:i:s', $value['del_time']).'</td>
				</tr>';
			}
			$this->ajaxReturn($html);
		} else {
			$this->ajaxReturn(false);

		}
	}


	/**
	 * [changeCheck 更改审查信息已读/未读状态]
	 * @return   [成功, ajax返回1 | 失败ajax返回0]
	 */
	public function changeCheck(){
		$check = M('check');
		$check->create();

		$res = $check->save();

		if ($res) {
			$this->ajaxReturn('1');
		} else {
			$this->ajaxReturn('0');
		}

	}


	/**
	 * [changeChe 改变审查信息状态, 以及ajax返回该条信息]
	 * @return   [修改成功, ajax返回该条信息 | 失败 ajax返回0]
	 */
	public function changeChe(){
		$c = M();
		$_POST['c_id'] = I('post.c_id');
		$_POST['if_read'] = I('post.if_read');
		$check = M('check');
		$c->startTrans();

		$list = $check->find($_POST['c_id']);

		$check->create();
		$res = $check->save();

		if ($res && $list) {
			$c->commit();

			$list['public_time'] = date('Y-m-d H:i:s', $list['public_time']);
			$list['del_time'] = date('Y-m-d H:i:s', $list['del_time']);

			$this->ajaxReturn($list);
		} else {
			$c->rollback();
			$this->ajaxReturn('0');
		}

	}

	/**
	 * [getOneCheck 获得一条未读的审查信息]
	 * @return [成功, ajax返回一条审查信息  |   失败, ajax返回发了se]
	 */
	public function getOneCheck(){
		$check = M('check');
		$user_id = $_SESSION['home']['uid'];
		$where['user_id'] = $user_id;
		$where['if_read'] = 1;
		$res = $check->field('c_id, user_id, cont_type, UNIX_TIMESTAMP(now())-del_time diff, public_time, if_read')->order('del_time desc')->where($where)->limit(7,1)->select();
		if ($res) {
			foreach ($res as $key => $value) {
				if ($value['diff']<60) {
					$res[$key]['diff'] = $value['diff'].'秒之前...';
				}else if($value['diff']<3600){
					$minu = floor($value['diff']/60);
					$res[$key]['diff'] = $minu.'分钟之前...';
				}else if($value['diff']<86400){
					$hour = floor($value['diff']/3600);
					$res[$key]['diff'] = $hour.'小时之前...';
				}else if($value['diff']<2592000){
					$day = floor($value['diff']/86400);
					$res[$key]['diff'] = $day.'天之前...';
				}else if($value['diff']<31104000){
					$mon = floor($value['diff']/2592000);
					$res[$key]['diff'] = $mon.'月之前...';
				}else{
					$year = floor($value['diff']/31104000);
					$res[$key]['diff'] = $year.'月之前...';
				}
				$res[$key]['public_time'] = date('Y-m-d H:i:s', $value['public_time']);
			}

			if ($res[0]['cont_type']==1) {
$html = <<<HELLO

			<li style="display:none">
				<a href="#" class="check_i" cid="{$res[0]['c_id']}">
					<span class="label label-danger">
						<i class="icon-bolt"></i>
					</span>
					[系统# 信息->警告]
					<span class="small italic">{$res[0]['diff']}</span>
				</a>
			</li>
HELLO;

			}else{

$html = <<<HELLO

			<li style="display:none">
				<a href="#" class="check_i" cid="{$res[0]['c_id']}">
					<span class="label label-warning">
						<i class="icon-bell"></i>
					</span>
					[系统# 图片->警告]
					<span class="small italic">{$res[0]['diff']}</span>
				</a>

			</li>

HELLO;
			}


			$this->ajaxReturn($html);
		}else{
			$this->ajaxReturn(false);
		}
	}

	/**
	 * [sendNews 透过调用接口,接收5条实时新闻]
	 */
	public function sendNews(){
		$ch = curl_init();
		$url = 'http://apis.baidu.com/songshuxiansheng/news/news';
		$header = array(
		'apikey: 4f7b67817e1dfc10780973a4b50fd429',
		);
		// 添加apikey到header
		curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// 执行HTTP请求
		curl_setopt($ch , CURLOPT_URL , $url);
		$res = curl_exec($ch);

		// var_dump(json_decode($res));
		$news_list = json_decode($res, true)['retData'];
		// var_dump($news_list);
		$this->assign('news_list', $news_list);
		// die;
	}

	/**
	 * [weather 请求接口, 查询天气]
	 * @return [type] [description]
	 */
	public function weather(){
		$ch = curl_init();
		$url = 'http://apis.baidu.com/heweather/weather/free?city=shanghai';
		$header = array(
			'apikey: 4f7b67817e1dfc10780973a4b50fd429',
		);
		// 添加apikey到header
		curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// 执行HTTP请求
		curl_setopt($ch , CURLOPT_URL , $url);
		$res = curl_exec($ch);

		$weather = json_decode($res, true)['HeWeather data service 3.0'][0];
		// var_dump($weather);
		$this->assign('weather', $weather);

	}

}
